package 代码记录.指定公司代码练习记录.华为_2024.分值100;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author zx
 * @create 2024-02-02 20:04
 * 这个题也有难度，没看懂，二刷
 */
public class 模拟目录管理功能_20 {
    public static void main(String[] args) {
        FileSystem fileSystem = new FileSystem();
        Scanner scanner = new Scanner(System.in);
        try {
            while (true) {
                String userInput = scanner.nextLine().trim();
                if (userInput.isEmpty()) {
                    continue;
                }
                fileSystem.executeCommand(userInput);
            }
        } catch (Exception e) {

        }
    }
    static class Directory {
        private String name;
        private Directory parent;
        private Map<String, Directory> subdirectories;

        public Directory(String name, Directory parent) {
            this.name = name;
            this.parent = parent;
            this.subdirectories = new HashMap<>();
        }

        public void addDirectory(String directoryName) {
            if (!subdirectories.containsKey(directoryName)) {
                Directory newDirectory = new Directory(directoryName, this);
                subdirectories.put(directoryName, newDirectory);
            }
        }

        public Directory changeDirectory(String directoryName) {
            if ("..".equals(directoryName)) {
                return (parent != null) ? parent : this;
            }

            return subdirectories.getOrDefault(directoryName, this);
        }

        public String getPath() {
            StringBuilder path = new StringBuilder(name);
            Directory current = parent;
            while (current != null) {
                path.insert(0, current.name + "/");
                current = current.parent;
            }
            return path.toString() + "/";
        }
    }

    static class FileSystem {
        private Directory root;

        public FileSystem() {
            this.root = new Directory("", null);
        }

        public void executeCommand(String command) {
            if (command.startsWith("mkdir")) {
                String directoryName = command.split("\\s+")[1];
                Directory currentDirectory = root.changeDirectory(directoryName);
                currentDirectory.addDirectory(directoryName);
            } else if (command.startsWith("cd")) {
                String directoryName = command.split("\\s+")[1];
                root = root.changeDirectory(directoryName);
            } else {
                System.out.println(root.getPath());
            }
        }
    }
}
